/*
 * @Author: heinan
 * @Date: 2023-07-23 22:50:43
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-01 21:53:53
 */
"use strict";
const { Controller } = require("egg");

/**
 *  @Controller 团购管理
 */
class GroupController extends Controller {
  /**
   * @Summary 团购列表接口
   * @Description 获取团购列表
   * @Router GET /api/v1/group
   * @Request query String house_name 楼盘名称
   * @Request query String province 省份
   * @Request query String city 城市
   * @Request query String county 区/县
   * @Request query String address 详细地址
   * @Request query Number group_status 团购进行状态
   * @Request query Number status 团购状态
   * @Request query Number currentPage 分页展示个数
   * @Request query Number pageSize 分页展示个数
   */
  async index() {
    console.log(this.ctx.query)
    const result = await this.ctx.service.group.index(this.ctx.query);
    if (result.data.length) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
        data: [],
      };
    }
  }
  /**
   * @Summary 添加团购数据接口
   * @Description 添加团购数据
   * @Router POST /api/v1/group
   * @Request body createGroup *body
   */
  async create() {
    const result = await this.ctx.service.group.create(this.ctx.request.body);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "新增成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "新增失败!",
      };
    }
  }
  /**
   * @Summary 更新团购数据接口
   * @Description 根据团购数据id,接口更新团购数据状态
   * @Router PUT /api/v1/group/{id}
   * @Request path string *id
   * @Request body updateGroupStatus *body
   */
  async update() {
    const result = await this.ctx.service.group.update({
      ...this.ctx.params,
      ...this.ctx.request.body,
    });
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败!",
      };
    }
  }
  /**
   * @Summary 删除团购接口
   * @Description 根据团购数据id删除接口
   * @Router DELETE /api/v1/group/{id}
   * @Request path string *id
   */
  async destroy() {
    const result = await this.ctx.service.group.destroy(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败!",
      };
    }
  }
}

module.exports = GroupController;
